/**
 * @license
 * Visual Blocks Editor
 *
 * Copyright 2016 Massachusetts Institute of Technology
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

"use strict";

goog.provide("Blockly.Blocks.motors");


goog.require("Blockly.Blocks");
goog.require("Blockly.Colours");
goog.require("Blockly.constants");
goog.require("Blockly.ScratchBlocks.VerticalExtensions");


Blockly.Blocks["motor_turnfordirection"] = {
  /**
   * Block to move steps.
   * @this Blockly.Block
   */
  init: function() {
    this.jsonInit({
      message0: Blockly.Msg.MOTOR_TURNFORDIRECTION,
      args0: [
        {
          type: "field_image",
          src: Blockly.mainWorkspace.options.pathToMedia + "icons/motors.svg",
          width: 27,
          height: 27,
        },
        {
          type: "input_value",
          name: "WORD",
        },
        {
          type: "input_value",
          name: "TURN_IMAGE",
        },
        // {
        //   type: "field_matrix_dropdown_image",
        //   width:15,
        //   name: "MATRIX",
        //   height:15,
        //   options:[ [Blockly.mainWorkspace.options.pathToMedia + "rotate-left.svg","URL_TO_IMAGE_1"],
        //     [Blockly.mainWorkspace.options.pathToMedia + "rotate-right.svg",  "URL_TO_IMAGE_2"]]
        // },
        {
          type: "input_value",
          name: "CIRCLE",
        },
        {
          type: "input_value",
          name: "GOTO",
        },
      ],
    
      category: Blockly.Categories.motors,
      extensions: ["colours_motors", "shape_statement"],
    });

    // 添加字段变更监听
    //  this.setOnChange(function(event) {
    //   if (event.type === Blockly.Events.BLOCK_CHANGE &&
    //       event.name === 'TURN' ) {
    //     // 获取DEGREES字段的输入值
    //     // var degreesInput = this.getInputTargetBlock('DEGREES');
        
    //     // if (degreesInput && degreesInput.type === 'math_number') {
    //       // 创建自定义方块
    //       var customBlock = Blockly.Block.obtain(this.workspace, 'motor_turnfordirection_turn_image');
    //       // 在方块下方插入自定义方块
    //       this.workspace.getBlockById(this.id).getInput('TURN').connection.connect(customBlock.previousConnection);
    //     // }
    //   }
    // });
  },
};






// Blockly.Blocks["motor_turnfordirection_word"] = {
//   /**
//    * Glide to drop-down menu
//    * @this Blockly.Block
//    */
//   init: function() {
//     this.jsonInit({
//       message0: "%1",
//       args0: [
//         {
//           type: "field_dropdown",
//           name: "WORD",
//           options: [
//             [Blockly.Msg.MOTOR_TURNFORDIRECTION_WORD_A, "_a_"],
//             [Blockly.Msg.MOTOR_TURNFORDIRECTION_WORD_B, "_b_"],
//           ],
//         },
//       ],
//       colour: Blockly.Colours.motors.secondary,
//       colourSecondary: Blockly.Colours.motors.secondary,
//       colourTertiary: Blockly.Colours.motors.tertiary,
//       extensions: ["output_string"],
//     });
//   },
// };



Blockly.Blocks["motor_turnfordirection_turn_image"] = {
  /**
   * Block to turn image.
   * @this Blockly.Block
   */
  init: function() {
    this.jsonInit({
      message0: "%1",
      args0: [
        {
          type: "field_image_dropdown",
          name: "TURN",
          options: [
            [Blockly.mainWorkspace.options.pathToMedia + "rotate-left.svg","URL_TO_IMAGE_1"],
            [Blockly.mainWorkspace.options.pathToMedia + "rotate-right.svg",  "URL_TO_IMAGE_2"]
          ],
        }
      ],
      colour: Blockly.Colours.motors.secondary,
      colourSecondary: Blockly.Colours.motors.secondary,
      colourTertiary: Blockly.Colours.motors.tertiary,
      extensions: ["output_string"],
    });
  },
};

Blockly.Blocks["motor_turnfordirection_to"] = {
  /**
   * Glide to drop-down menu
   * @this Blockly.Block
   */
  init: function() {
    this.jsonInit({
      message0: "%1",
      args0: [
        {
          type: "field_dropdown",
          name: "GOTO",
          options: [
            [Blockly.Msg.MOTOR_TURNFORDIRECTION_TO_CIRCLE, "_mouse_"],
            [Blockly.Msg.MOTOR_TURNFORDIRECTION_TO_DEGREE, "_random_"],
            [Blockly.Msg.MOTOR_TURNFORDIRECTION_TO_SECONDS, "_seconds_"],
          ],
        },
      ],
      colour: Blockly.Colours.motors.secondary,
      colourSecondary: Blockly.Colours.motors.secondary,
      colourTertiary: Blockly.Colours.motors.tertiary,
      extensions: ["output_string"],
    });
  },
};

Blockly.Blocks["motor_godirection_toposition"] = {
  /**
   * Block to turn right.
   * @this Blockly.Block
   */
  init: function() {
    this.jsonInit({
      message0: Blockly.Msg.MOTOR_GODIRECTION_TOPOSITION,
      args0: [
        {
          type: "field_image",
          src: Blockly.mainWorkspace.options.pathToMedia + "icons/motors.svg",
          width: 27,
          height: 27,
        },
        {
          type: "input_value",
          name: "POSITION_WORD",
        },
        {
          type: "input_value",
          name: "POSITION_DIRECTION",
        },
        {
          type: "input_value",
          name: "POSITION_POSITION",
        },
      ],
      category: Blockly.Categories.motors,
      extensions: ["colours_motors", "shape_statement"],
    });
  },
};

Blockly.Blocks["motor_godirection_toposition_direction"] = {
  /**
   * Glide to drop-down menu
   * @this Blockly.Block
   */
  init: function() {
    this.jsonInit({
      message0: "%1",
      args0: [
        {
          type: "field_dropdown",
          name: "POSITION_DIRECTION",
          options: [
            [Blockly.Msg.MOTOR_TURNFORDIRECTION_WORD_CLOCKWISE, "_clockwise_"],
            [Blockly.Msg.MOTOR_TURNFORDIRECTION_WORD_ANTICLOCKWISE, "_anticlockwise_"],
            [Blockly.Msg.MOTOR_TURNFORDIRECTION_WORD_SHORTPATH, "_shortpath_"],
          ],
        },
      ],
      colour: Blockly.Colours.motors.secondary,
      colourSecondary: Blockly.Colours.motors.secondary,
      colourTertiary: Blockly.Colours.motors.tertiary,
      extensions: ["output_string"],
    });
  },
};


Blockly.Blocks["motor_start_direction"] = {
/**
   * Block to start direction.
   * @this Blockly.Block
   */
  init: function() {

    this.jsonInit({
      message0: Blockly.Msg.MOTOR_STARTDIRECTION,
      args0: [
        {
          type: "field_image",
          src: Blockly.mainWorkspace.options.pathToMedia + "icons/motors.svg",
          width: 27,
          height: 27,
        },
        {
          type: "input_value",
          name: "WORD",
        },
        {
          type: "input_value",
          name: "TURN_IMAGE",
        },
      ],
      category: Blockly.Categories.motors,
      extensions: ["colours_motors", "shape_statement"],
    });
  },
};

Blockly.Blocks["motor_end_direction"] = {
  /**
     * Block to start direction.
     * @this Blockly.Block
     */
  init: function() {
  
    this.jsonInit({
      message0: Blockly.Msg.MOTOR_ENDDIRECTION,
      args0: [
        {
          type: "field_image",
          src: Blockly.mainWorkspace.options.pathToMedia + "icons/motors.svg",
          width: 27,
          height: 27,
        },
        {
          type: "input_value",
          name: "WORD",
        },
      ],
      category: Blockly.Categories.motors,
      extensions: ["colours_motors", "shape_statement"],
    });
  },
};

// Blockly.Blocks["motor_toposition_word"] = {
//   /**
//    * Glide to drop-down menu
//    * @this Blockly.Block
//    */
//   init: function() {
//     this.jsonInit({
//       message0: "%1",
//       args0: [
//         {
//           type: "field_dropdown",
//           name: "POSITION_WORD",
//           options: [
//             [Blockly.Msg.MOTOR_TURNFORDIRECTION_WORD_A, "_a_"],
//             [Blockly.Msg.MOTOR_TURNFORDIRECTION_WORD_B, "_b_"],
//           ],
//         },
//       ],
//       colour: Blockly.Colours.motors.secondary,
//       colourSecondary: Blockly.Colours.motors.secondary,
//       colourTertiary: Blockly.Colours.motors.tertiary,
//       extensions: ["output_string"],
//     });
//   },
// };

Blockly.Blocks["motor_settings_speed"] = {
  /**
   * Block to turn right.
   * @this Blockly.Block
   */
  init: function() {
    this.jsonInit({
      message0: Blockly.Msg.MOTOR_SETTINGS_SPEED,
      args0: [
        {
          type: "field_image",
          src: Blockly.mainWorkspace.options.pathToMedia + "icons/motors.svg",
          width: 27,
          height: 27,
        },
        {
          type: "input_value",
          name: "POSITION_WORD",
        },
       
        {
          type: "input_value",
          name: "CIRCLE",
        },
      ],
      category: Blockly.Categories.motors,
      extensions: ["colours_motors", "shape_statement"],
    });
  },
};


Blockly.Blocks['custom_block_with_buttons'] = {
  init: function() {
    this.jsonInit({
      "message0": "Select %1",
      "args0": [
        {
          "type": "field_dropdown_with_buttons",
          "name": "OPTIONS",
          "options": [
            ["Option 1", "OPTION1"],
            ["Option 2", "OPTION2"]
          ]
        }
      ],
      "colour": 160,
      "tooltip": "A block with a custom dropdown field and buttons.",
      "helpUrl": ""
    });
  }
};

Blockly.Blocks["motor_settings_position"] = {
  /**
     * Block to start direction.
     * @this Blockly.Block
     */
  init: function() {
  
    this.jsonInit({
      message0: Blockly.Msg.MOTOR_SETTINGS_POSITION,
      args0: [
        {
          type: "field_image",
          src: Blockly.mainWorkspace.options.pathToMedia + "icons/motors.svg",
          width: 27,
          height: 27,
        },
        {
          type: "input_value",
          name: "WORD",
        },
      ],
      category: Blockly.Categories.motors,
      extensions: ["colours_motors","output_number"],
    });
  },
};

Blockly.Blocks["motor_speed"] = {
  /**
     * Block to start direction.
     * @this Blockly.Block
     */
  init: function() {
  
    this.jsonInit({
      message0: Blockly.Msg.MOTOR_SPEED,
      args0: [
        {
          type: "field_image",
          src: Blockly.mainWorkspace.options.pathToMedia + "icons/motors.svg",
          width: 27,
          height: 27,
        },
        {
          type: "input_value",
          name: "WORD",
        },
      ],
      category: Blockly.Categories.motors,
      extensions: ["colours_motors","output_number"],
    });
  },
};

Blockly.Blocks["color_select"] = {
  /**
     * Block to start direction.
     * @this Blockly.Block
     */
  init: function() {
  
    this.jsonInit({
      message0: "%1 %2",
      args0: [
        {
          type: "field_image",
          src: Blockly.mainWorkspace.options.pathToMedia + "icons/motors.svg",
          width: 27,
          height: 27,
        },
        {
          type: "input_value",
          name: "MATRIX_COLOR_SELECT",
        },
      ],
      category: Blockly.Categories.motors,
      extensions: ["colours_motors","output_number"],
    });
  },
};












